
from unittest import result
from utils.db_util import SqliteUtil


def add_weibos(uid,weibos):
    sql = SqliteUtil(uid)
    sql.execute("create table if not exists articles (uid int not null,blog_id int primary key not null ,name chat(50) not null,content text,url text,created_at text,has_push bool default false );")

    result =  sql.execute("insert or replace into articles (uid,name,blog_id,content,url,created_at,has_push) values (?,?,?,?,?,?,?);",weibos)
    sql.close()

def add_weibo(uid,name,blog_id,content,url,created_at,has_push=False):
    sql = SqliteUtil(uid)
    sql.execute("create table if not exists articles (uid int not null,blog_id int primary key not null ,name chat(50) not null,content text,url text,created_at text,has_push bool default false );")

    query_result = sql.query("select * from articles where blog_id=?;",(blog_id,))
    if len(query_result)==0:
        result =  sql.execute("insert into articles (uid,name,blog_id,content,url,created_at,has_push) values (?,?,?,?,?,?,?);",(uid,name,blog_id,content,url,created_at,has_push))
    sql.close()

def has_push_weibo(uid,blog_id,has_push):
    sql = SqliteUtil(uid)
    sql.execute("update articles set has_push=? where blog_id=?",(has_push,blog_id,))
    sql.close()

def get_all_weibo(uid):
    sql = SqliteUtil(uid)
    result = sql.query("select * from articles;")
    sql.close()
    return result

# get_all_weibo('123')
#add_weibo(123,'清欢',4455,"内容内容","http://baidu.com","1649582156947",True)
#add_weibo(123,'清欢',9988,"内容内容","http://qq.com","1649582156947",False)
#has_push_weibo(123,9988,True)

